% clear workspace
clear

% Test data set
A = [2 2 4 4; 6 0 6 0];
B = [8 8 10 10; 2 4 2 4];
% a priori probabilities
prior = [0.5 0.5];

% calculate mean values
meanA = mean(A,2)
meanB = mean(B,2)

% create the needed X and Y values for the visualization
meanX = [meanA(1,1) meanB(1,1)];
meanY = [meanA(2,1) meanB(2,1)];

% concatenate the test data for the scatterplot
C1 = horzcat(A, B);
D1 = C1';
% create a grouping vector
G1 = [1 1 1 1 2 2 2 2];

% create data for the classifier
si1 = [1 1];        % use identity matrix
% si1 = cov(A, B);    % use general cov-matrix

%data1 = mvnrnd(meanA', si1, 100);
%data2 = mvnrnd(meanB', si1, 100);
%sample = vertcat(data1, data2);

% use classify to get the discriminate function using the covariance matrix
[C,err,P,logp,coeff] = classify(D1, D1, G1, 'diagquadratic', prior);
% get the coefficients of the discriminate function
K = coeff(1,2).const;
L = coeff(1,2).linear; 
Q = coeff(1,2).quadratic;
% build a function for ezplot
f = @(x,y) K + [x y]*L + sum(([x y]*Q) .* [x y], 2);

% now visualize the results
figure
hold on

% scatter plot the test data
gscatter(D1(:,1), D1(:,2), G1);

% plot the discriminate functions
h2 = ezplot(f,[4 7 0 6]);

% plot the mean points
plot(meanX, meanY, 'g.');

% draw a line between the means
line(meanX, meanY);

% done
hold off